package com.yichuang.wuziqi.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;

import com.yichuang.wuziqi.R;
import com.yichuang.wuziqi.game.LocalGame;
import com.yichuang.wuziqi.player.AI;
import com.yichuang.wuziqi.player.LocalAI;
import com.yichuang.wuziqi.player.LocalPlayer;
import com.yichuang.wuziqi.player.Playable;
import com.yichuang.wuziqi.player.Player;
import com.yichuang.wuziqi.view.BoardPanel;

public class LocalGameActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "xcm";

    private Button startBtn;
    private Button endBtn;
    private BoardPanel boardPanel;
    private LocalGame currentGame;

    private Spinner whitePlayerSpinner;
    private Spinner blackPlayerSpinner;

    private Player whitePlayer;
    private Player blackPlayer;

    private int AILevel = AI.LEVEL_EASY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.local_game);
        initView();
    }

    private void initView() {
        startBtn = findViewById(R.id.start);
        endBtn = findViewById(R.id.admit_beaten);
        startBtn.setOnClickListener(this);
        endBtn.setOnClickListener(this);
        endBtn.setEnabled(false);
        boardPanel = findViewById(R.id.panel);
        whitePlayerSpinner = findViewById(R.id.spinner_white);
        blackPlayerSpinner = findViewById(R.id.spinner_black);

        whitePlayerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        whitePlayer = new LocalPlayer(Playable.WHITE_SIDE, boardPanel);
                        break;
                    case 1:
                        whitePlayer = new LocalAI(Playable.WHITE_SIDE, LocalGame.MAX_LINE,AI.LEVEL_EASY);
                        break;
                    case 2:
                        whitePlayer = new LocalAI(Playable.WHITE_SIDE, LocalGame.MAX_LINE,AI.LEVEL_MIDDLE);
                        break;
                    case 3:
                        whitePlayer = new LocalAI(Playable.WHITE_SIDE, LocalGame.MAX_LINE,AI.LEVEL_HARD);
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        blackPlayerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        blackPlayer = new LocalPlayer(Playable.BLACK_SIDE, boardPanel);
                        break;
                    case 1:
                        blackPlayer = new LocalAI(Playable.BLACK_SIDE, LocalGame.MAX_LINE,AI.LEVEL_EASY);
                        break;
                    case 2:
                        blackPlayer = new LocalAI(Playable.BLACK_SIDE, LocalGame.MAX_LINE,AI.LEVEL_MIDDLE);
                        break;
                    case 3:
                        blackPlayer = new LocalAI(Playable.BLACK_SIDE, LocalGame.MAX_LINE,AI.LEVEL_HARD);
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                boardPanel.reset();

                currentGame = new LocalGame();
                currentGame.setRecorder(boardPanel);
                currentGame.setPlayer(whitePlayer, blackPlayer);

                new Thread(currentGame).start();
                startBtn.setEnabled(false);
                endBtn.setEnabled(true);
                break;
            case R.id.admit_beaten:
                currentGame.setGameOver(true);
                startBtn.setEnabled(true);
                endBtn.setEnabled(false);
                break;
        }
    }
}
